package log

import (
	"fmt"
	"github.com/cloudwego/hertz/pkg/common/hlog"
	hertzlogrus "github.com/hertz-contrib/logger/logrus"
	"github.com/sirupsen/logrus"
	"log"
	"os"
	"path"
)

func NewLogrusLog(config *Config) (logger *hertzlogrus.Logger) {
	var logFilePath string
	logFilePath = config.path + "/"
	if err := os.MkdirAll(logFilePath, 0o777); err != nil {
		log.Println(err.Error())
		return
	}

	logFileName := fmt.Sprintf("%s.log", config.filename)
	fileName := path.Join(logFilePath, logFileName)
	if _, err := os.Stat(fileName); err != nil {
		if _, err := os.Create(fileName); err != nil {
			log.Println(err.Error())
			return
		}
	}

	lr := logrus.New()
	lr.SetFormatter(config.logrusFormatter)
	logger = hertzlogrus.NewLogger(hertzlogrus.WithLogger(lr))

	// 提供压缩和删除
	logger.SetOutput(config.lumberjackLogger)
	logger.SetLevel(hlog.LevelDebug)
	return
}
